package com.yuan.yblog.portal.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuan.yblog.common.api.CommonResult;
import com.yuan.yblog.crud.entity.Post;
import com.yuan.yblog.portal.aop.annotation.Log;
import com.yuan.yblog.portal.aop.annotation.SlidingWindowLimiter;
import com.yuan.yblog.portal.enums.BusinessType;
import com.yuan.yblog.portal.enums.LimitType;
import com.yuan.yblog.portal.pojo.VO.PostRankVO;
import com.yuan.yblog.portal.pojo.VO.PostItemVO;
import com.yuan.yblog.portal.pojo.VO.PostVO;
import com.yuan.yblog.portal.service.PostService;
import com.yuan.yblog.portal.service.WeekRankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Post)表控制层
 * @since 2023-12-03 14:21:18
 */
@RestController
@RequestMapping("/post")
@Tag(name = "社区文章（post）接口")
public class PostController {

    @Autowired
    private PostService postService;

    @Autowired
    WeekRankService weekRankService;

    @SlidingWindowLimiter(key = "post", windowSize = 120, windowTimeRange = 60, limitType = LimitType.IP)
    @Operation(summary = "按“列表”方式批量获取文章")
    @GetMapping("/list")
    public CommonResult<IPage<PostItemVO>> list(
                                        @Parameter(description = "分类id") @RequestParam(required = false) Long categoryId,
                                        @Parameter(description = "置顶等级") @RequestParam(required = false) Long level,
                                        @Parameter(description = "作者id") @RequestParam(required = false) Long userId,
                                        @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long pageNumber,
                                        @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Long pageSize,
                                        @Parameter(description = "排序字段") @RequestParam(required = false) String sortKey,
                                        @Parameter(description = "是否降序") @RequestParam(required = false) Boolean isDesc) {
        IPage<PostItemVO> result = postService.postItemList(level, userId, categoryId, pageNumber, pageSize, sortKey, isDesc);
        return CommonResult.success(result);
    }

    @SlidingWindowLimiter(key = "post", windowSize = 120, windowTimeRange = 60, limitType = LimitType.IP)
    @Operation(summary = "按“文章id”获取文章详情")
    @GetMapping("/details/{id}")
    public CommonResult<Post> selectOne(@Parameter(description = "文章id") @PathVariable Long id) {
        Post post = postService.selectByIdAndBuildCache(id);
        return CommonResult.success(post);
    }


    @Log(title = "发布文章", businessType = BusinessType.INSERT)
    @Operation(summary = "发布新文章")
    @PostMapping("/insert")
    public CommonResult<Long> insert(@Parameter(description = "文章提交实体对象") @RequestBody PostVO postVO) {
        Long id = postService.insertPost(postVO);
        if (id != null) {
            return CommonResult.success(id,"发布成功，对象id=" + id);
        } else {
            return CommonResult.failed("发布失败");
        }
    }

    @Log(title = "编辑文章", businessType = BusinessType.UPDATE)
    @Operation(summary = "编辑旧文章")
    @PostMapping("/update")
    public CommonResult<Long> update(@Parameter(description = "文章提交实体对象") @RequestBody PostVO postVO) {
        Long id = postVO.getId();
        if (id == null) {
            return CommonResult.failed("id不能为空");
        }
        long count = postService.updatePost(postVO);
        if (count > 0) {
            return CommonResult.success(id,"提交成功");
        }
        return CommonResult.failed("提交失败");
    }

    @Log(title = "删除文章", businessType = BusinessType.DELETE)
    @Operation(summary = "根据文章id，逻辑删除文章")
    @DeleteMapping("/delete")
    public CommonResult<Integer> deleteById(@Parameter(description = "文章id")@RequestParam("postId") Long id) {
        boolean succeed = postService.deletePost(id);
        if (succeed) {
            return CommonResult.success(1,"成功删除数量: " + 1);
        } else {
            return CommonResult.failed("删除失败！");
        }
    }

    @SlidingWindowLimiter(key = "post", windowSize = 120, windowTimeRange = 60, limitType = LimitType.IP)
    @Operation(summary = "获取文章周榜")
    @GetMapping("/rankList")
    public CommonResult<List<PostRankVO>> postRankingList(){
        List<PostRankVO> result = weekRankService.getRankList();
        return CommonResult.success(result);
    }
}

